package com.tarena.lbs.basic.web.rpc;

import com.tarena.lbs.basic.api.BasicApi;
import com.tarena.lbs.basic.web.service.AdminService;
import com.tarena.lbs.basic.web.service.UserService;
import com.tarena.lbs.pojo.basic.dto.AdminDTO;
import com.tarena.lbs.pojo.basic.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@DubboService
@Component
@Slf4j
public class BasicApiImpl implements BasicApi {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    /**
     * RPC实现方法 一般调用业务来完成 将这个rpc实现看做一个接口的入口类
     * 调用业务方法 所产生的任何异常 由内部消化处理,不要向外抛出
     * @param adminId
     * @return
     */
    @Override
    public AdminDTO getAdminDetail(Integer adminId) {
        log.info("dubbo调用basic查询admin,入参:{}",adminId);
        AdminDTO adminDTO=null;
        try{
            adminDTO=adminService.detail(adminId);
        }catch (Exception e){
            log.error("获取商家账号详情失败",e);
        }
        log.info("dubbo调用basic查询admin,入参:{},出参:{}",adminId,adminDTO);
        return adminDTO;
    }

    @Override
    public List<Integer> getUserGroupIds(Integer userId, Integer businessId) {
        List<Integer> groupIds=null;
        try{
            groupIds=userService.getUserGroups(userId,businessId);
        }catch (Exception e){
            log.error("查询用户:{}所属商家:{}的人群列表失败",userId,businessId,e);
        }
        return groupIds;
    }

    @Override
    public UserDTO userDetail(Integer userId) {
        log.info("dubbo调用basic查询用户详情,入参:{}",userId);
        UserDTO userDTO=null;
        try{
            userDTO=userService.detail(userId);
        }catch (Exception e){
            log.error("获取用户详情失败",e);
        }
        return userDTO;
    }
}
